package a.b.mvc6;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.List;

/**
 * 这里配置Spring(非web)相关的配置
 */
@Configuration
// 扫描非controller类
@ComponentScan({
        "a.b.mvc6.config",
        "a.b.mvc6.service",
        "a.b.mvc6.mapper",
        "a.b.mvc6.utils"
})
@EnableScheduling // 开启定时任务
@EnableAsync // 开启异步编程支持
// 读取资源文件
@PropertySource(value = "classpath:application.properties", encoding = "UTF-8")
public class SpringConfig {

    @Autowired
    private Environment env;

    /**
     * 定义 ThreadPoolTaskScheduler 用于 定时任务
     * @return ThreadPoolTaskScheduler
     */
    @Bean(destroyMethod = "shutdown")
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(Integer.parseInt(env.getProperty("scheduler.pool-size", "16")));
        scheduler.setThreadNamePrefix(env.getProperty("scheduler.thread-name-prefix", "scheduler-"));
        scheduler.setAwaitTerminationSeconds(Integer.parseInt(env.getProperty("scheduler.await-termination-seconds", "60")));
        scheduler.setWaitForTasksToCompleteOnShutdown(Boolean.parseBoolean(env.getProperty("scheduler.wait-for-tasks-to-complete-on-shutdown", "true")));
        scheduler.afterPropertiesSet();
        return scheduler;
    }

    /**
     * 定义 ThreadPoolTaskExecutor，用于 异步方法
     * @return ThreadPoolTaskExecutor
     */
    @Bean(destroyMethod = "shutdown")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(Integer.parseInt(env.getProperty("executor.core-pool-size", "5")));
        executor.setMaxPoolSize(Integer.parseInt(env.getProperty("executor.max-pool-size", "10")));
        executor.setQueueCapacity(Integer.parseInt(env.getProperty("executor.queue-capacity", "25")));
        executor.setThreadNamePrefix(env.getProperty("executor.thread-name-prefix", "executor-"));
        executor.setKeepAliveSeconds(Integer.parseInt(env.getProperty("executor.keep-alive-seconds", "60")));
        executor.setAwaitTerminationSeconds(Integer.parseInt(env.getProperty("executor.await-termination-seconds", "60")));
        executor.setWaitForTasksToCompleteOnShutdown(Boolean.parseBoolean(env.getProperty("executor.wait-for-tasks-to-complete-on-shutdown", "true")));
        executor.afterPropertiesSet();
        return executor;
    }
}
